package com.jl.slick.objects.graphics;

import java.util.HashMap;
import java.util.Vector;
import java.awt.Point;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.imageout.ImageIOWriter;
import org.newdawn.slick.imageout.ImageOut;

import com.jl.slick.objects.logic.FX;
public class Level implements Serializable
{

	private static final long serialVersionUID = 1L;
	
	transient private Image img;
	private HashMap<Point, FX> AllFX;
	private Vector<Point> Collide;
	private Point begin, end;
	private String name;
	
	public Level(Image img, Point begin, Point end, String name)
	{
		this.img = img;
		this.begin = begin;
		this.end = end;
		this.name = name;
	}
	
	
	public void setCollide(Vector<Point> Collide)
	{
		this.Collide = Collide;
	}
	
	public Image getWorld()
	{
		return this.img;
	}
	
	public void setWorld(Image img)
	{
		this.img = img;
	}
	
	public Point getBegin()
	{
		return this.begin;
	}
	
	public Point getEnd()
	{
		return this.end;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public Vector<Point> getCollide()
	{
		return this.Collide;
	}
	
	static public void writeOut(Level towrite)
	{
		String filename = towrite.getName();
	      OutputStream file = null;
	      OutputStream buffer = null;
	      ObjectOutput output = null;
	      ImageIOWriter imgwrite = new ImageIOWriter();
		try{
		      //use buffering
			file = new FileOutputStream( filename );
			buffer = new BufferedOutputStream( file );
			output = new ObjectOutputStream( buffer );
		    output.writeObject(towrite);
		 //   file = new FileOutputStream(filename + "img.png");
		    ImageOut.write(towrite.getWorld(), ImageOut.PNG, filename + "img.png");
		    }  
		    catch(IOException ex)
		    {
		     System.out.println("Error writing level to file.");
		     ex.printStackTrace();
		    } catch (SlickException e) {
			     System.out.println("Error writing image. see stack trace: ");
				e.printStackTrace();
			}
	      finally{
		        try {
					output.close();
				} catch (IOException e) {
					System.err.println("Error closing output stream. still in use? see stack trace: ");
					e.printStackTrace();
				}
		      }
 
	}
	
	static public Level readIn(String name)
	{
		Level returnval = null;
		try
		{
			Image loadedimage = new Image("levels/" + name + "img.png");
			FileInputStream fin = new FileInputStream("levels/" + name);
			ObjectInputStream ois = new ObjectInputStream(fin);
			returnval = (Level) ois.readObject();
			returnval.setWorld(loadedimage);
			
		}
		catch (SlickException e)
		{
			System.out.println("Couldn't load Image for level");
			e.printStackTrace();
		} 
		catch (ClassNotFoundException e) 
		{
			System.out.println("File may be corrupt. Check stack trace");
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			System.out.println("Error loading file. check stack trace");
			e.printStackTrace();
		}

		return returnval;
	}
	
	
}
